2-2

Physicist's Introduction to Mathematica 6

Third Edition 2007

PHYSICS 200

Nicholas Wheeler

Laboratory 1

Basic  Graphics

Pen-&-ink mathematicians have traditionally found graphics so labor intensive that they have tended to avoid figures whenever possible…and, when a figure could not be avoided, to make do with a merely qualitative representation of the points at issue.  Figures have tended to see service only as expository devices, used to illustrate what was already known, and seldom to be used as aids to discovery.

Mathematica makes the production of accurate figures—of many types—so quick and easy that it should/will become your habit to look to the graphical aspects of problems even before you have fully understood them. The graphical aspects of your mathematical work will therefore assume enlarged importance.  Graphics—formerly an expository device—has become an exploratory tool.

In today's lab we look to some of the most basic graph-generating procedures. In Lab 2 we will look to some more advanced procedures.

Resources

Your principal reference is at your fingertips. Go to Help > Documentation Center and click on Visualization and Graphics. You will be presented with a list of ten topical heads. Be content for now to open and consider the links made available under the first four of those heads:

Look around to get a sense of the richness of this resource. You might, for example, open Functional Visualization and click on Plot. You will be told how the command works, and be provided with examples. Pay attention also to the tutorials (click on, for example, Basic Plotting), related links and more about that appear at the bottom of the page. You cannot possibly explore all the information placed thus at your command, but these are steps you will have frequent occasion to repeat while familiarizing yourself with Mathematica, and also even after you have become expert, for the small but critical details do tend to slip away.

It might be a good idea to keep a My Graphics notebook, recording your successful accomplishments; it is likely to become your primary reference, a source of templates that you know from your own experience work. Such a notebook would acquire value especially with regard to the correct management of the numerous "options" which are the main source of confusion in this area.

Simple 2-Dimensional Plotting

?Plot

First Example

Plot[Tan[x],{x,0,20}]

Notice that Mathematica has no trouble at all with the asymptotic infinities.

We look now to a few basic elaborations of the preceding command. First we place the ticks—what are "ticks"?

?Ticks

—at trigonometrically more natural positions:

Plot[Tan[x],{x,0,20}, Ticks->{{π,2π,3π,4π,5π,6π},{-4,-2,2,4}}]

Next we increase the "thickness" of the curves:

?Thickness
?AbsoluteThickness

NOTE: Absolute thickness is measured in printer's points: one point = one 72nd of an inch.

Plot[Tan[x],{x,0,20}, Ticks->{{π,2π,3π,4π,5π,6π},{-4,-2,2,4}}, PlotStyle->{AbsoluteThickness[2]}]

Finally, we change the color of the curves:

Plot[Tan[x],{x,0,20}, Ticks->{{π,2π,3π,4π,5π,6π},{-4,-2,2,4}}, PlotStyle->{AbsoluteThickness[2],Red}]

Red, Green and Blue are basic color attributes. For information about more refined degrees of color control, trace the »s in

?RGBColor
?Hue

Plot[Tan[x],{x,0,20}, Ticks->{{π,2π,3π,4π,5π,6π},{-4,-2,2,4}}, PlotStyle->{AbsoluteThickness[2],RGBColor[.3,.5,.2]}]

NOTE: To create  → while entering a command, type first - (hyphen), then > .

Second Example

Plot[Sin[t](E^(-t/10)),{t,0,20π}]

Mathematica strives automatically to emphasize what it considers to be "the interesting part" of a figure, with the result that it has here clipped off some detail. I give two ways to override that defect:

Plot[Sin[t](E^(-t/10)),{t,0,20π}, PlotRange->All]
Plot[Sin[t](E^(-t/10)),{t,0,20π}, PlotRange->{-1,1}]

Third Example

This example, borrowed from §1.2 of Stan Wagon's Mathematica in Action (2nd edition 1999), provides a taste of "graphical discovery/exploration."

Plot[Sin[x]+ArcSin[x], {x,-0.85,0.85},Frame->True, FrameTicks->{{-0.8,0.8}, {-1,0,1}}]

The graph is remarkable for its unexpected linearity. To gain a sense of how linear it truly is, we might proceed as follows: we find the equation of the straight line that approximates the preceding curve in the neighborhood of its mid-section

Clear[a]

a=Sin[.5]+ArcSin[.5]

linear[x_]:=(a/(.5))x  (*Equation of a straight line passing through a couple of selected points on the curve*)

NOTE the technique used to attach an explanatory remark to the definition.

and then superimpose the curve and line:

Plot[{Sin[x]+ArcSin[x],linear[x]}, {x,-0.85,0.85},Frame->True, FrameTicks->{{-0.8,0.8}, {-1,0,1}}]

which reveals a slight departure from linearity at the ends. The mystery is explained when we command

Series[Sin[x]+ArcSin[x],{x,0,15}]

and discover that—remarkably—the Maclaurin expansion has no terms of orders 2, 3, or 4. We made first-time use here of the following resource:

?Series

The dangling O(([x])^16) is an uninformative annoyance, but is easily  avoided:

Series[Sin[x]+ArcSin[x],{x,0,15}]//Normal

Filled Plots

Go to Help > Documentation Center, launch a search for ref/Plot (which will take you back to a tutorial notebook you have already seen), scroll down the page a short distance and encounter examples of several kinds of "filled plots." Here follows an example of the utility of that device, taken from some of my own work.

In Gaussian Wavepackets (1998) I had occasion to notice that the following function

sechsquared[x_]:=(1/2)((Sech[x])^2)

has all the properties required of a probability distribution: it is everywhere non-negative, and

(∫_(-∞)^∞)sechsquared[x]\[DifferentialD]x

Moreover

(∫_(-∞)^∞)x sechsquared[x]\[DifferentialD]x
(∫_(-∞)^∞)(x^2) sechsquared[x]\[DifferentialD]x

and when plotted it looks very Gaussian (very "normal"):

Plot[sechsquared[x],{x,-4,4}]

The Gaussian with that same mean (which is to say: is centered at x = 0) and variance σ is defined

gaussian[x_,σ_]:=(1/(σ(Sqrt[2π])))Exp[-(1/2)((((x/σ)))^2)]

We want to compare the preceding figure with

Plot[gaussian[x,((π^2)/12)],{x,-4,4}]

so again superimpose the two figures, drawing upon

?Filling

to construct the command

Plot[{sechsquared[x], gaussian[x,((π^2)/12)]}, {x, -4,4}, Filling -> {1 -> {2}}]

Polar Plot

For relevant information, command

?PolarPlot

and follow the ≫.

Not infrequently the result of physical calculation lends itself better to polar than to Cartesian graphical representation. For example, the theory of planetary motion (Kepler problem) leads to an equation of the form

r=(constant/(1+eccentricity×cos(θ)))

of which I illustrate an instance:

PolarPlot[(1/(1+.8Cos[θ])),{θ,0,2π}]

Slight adjustment causes the orbit to precess:

PolarPlot[(1/(1+.8Cos[1.01θ])),{θ,0,20π}]

Try these further examples of polar plots:

PolarPlot[{t,(t^1.1),(t^1.2)},{t,0,2π}]
PolarPlot[Sin[5θ],{θ,0,2π}]

List Plot

For relevant information, pose the question

?ListPlot

and again go where the ≫ takes you. I illustrate this graphical technique as it pertains to the familiar Fibonacci numbers, which are available as a built-in resource:

?Fibonacci

Fibonacci[50]

Make a table of the first 10 Fibonacci numbers

fib=Table[Fibonacci[n],{n,10}]

and use  ListPlot to display that data graphically:

ListPlot[{1,1,2,3,5,8,13,21,34,55},PlotStyle->PointSize[Large], Ticks->{{1,2,3,4,5,6,7,8,9,10},{5,8,13,21,34,55}}]

I don't like where Mathematica elected to place the vertical axis, so look at the available options

Options[ListPlot]

which leads me to inquire about the precise meaning of

?AxesOrigin

and therefore to make this adjusted command:

ListPlot[{1,1,2,3,5,8,13,21,34,55},PlotStyle->PointSize[Large], Ticks->{{1,2,3,4,5,6,7,8,9,10},{5,8,13,21,34,55}},AxesOrigin->{0,0}]

Parametric Plot

Pose the question

?ParametricPlot

and follow the ≫. You are led to discription of a very powerful graphic resource, use of which I illustrate with a series of examples:

First Example: Simple Cycloid

A circle of unit radius rolls along the x-axis. We are interest in the curve traced by a point P marked on the circumference of the circle. Working from a sketch, we are led to define

x[θ_]:=θ-Sin[θ]
y[θ_]:=1-Cos[θ]

ParametricPlot[{x[θ], y[θ]}, {θ,0,6π}, Ticks->{{2π, 4π, 6π},{2}}]

REMARK: One can change the display size a graphical image by clicking on it and dragging a handle of the box that appears: try it!

Second Example: Epicycles

The following is taken from Stan Wagon's discussion of an idea developed by Norton Starr (Amherst College): A wheel of given radius turns with given angular velocity. Pinned at its circumference is a second wheel of given radius that turns with given angular velocity.  And pinned to its circumference is a third wheel…etc. We are interested in the motion of a point marked on the circumference of the last wheel.  In a specific instance, Wagon was led thus to the following command/figure:

ParametricPlot[{Cos[θ],Sin[θ]}+(1/2){Cos[7θ],Sin[7θ]}+(1/3){Cos[-17θ+(π/2)],Sin[-17θ+(π/2)]},{θ,0,2π},Frame->True]

Adjustment of the numerics would, of course, result in different curves.  It is not immediately obvious why the present numerics (1, 7 & —17) generate 6-fold symmetry!

Third Example: Lissajous Figures

The following construction is familiar to anyone who has ever played with an oscilloscope:

Clear[a,b,x,y]

x[t_,a_,α_]:=a Cos[α t]
y[t_,b_,β_,δ_]:=b Cos[β t+δ]

ParametricPlot[{x[t,2,2],y[t,1,3.00,π/2]},{t,0,16π},AspectRatio->Automatic]

ParametricPlot[{x[t,2,2],y[t,1,3.01,π/2]},{t,0,16π},AspectRatio->Automatic]

3-Dimensional Plotting

For relevant information, proceed in the usual manner: pose the question

?Plot3D

and follow the ≫.

The basic command is again very simple:

Clear[f]

f[x_,y_]:=((x^2)+3(y^2))(E^(1-(x^2)-(y^2)))

We will find it convenient to give a name to the resulting 3D figure:

doublehump=Plot3D[f[x,y],{x,-2,2},{y,-3,3}]

Show[doublehump]

Now something absolutely amazing—new to v.6: place the cursor on the figure, it turns into a pair of arrows, chasing one another around an ellipse. Drag that modified cursor around and the figure revolves, so that you can few the plotted surface from all angles, from above and below!

Plot3D is endowed with a great many control options

Options[Plot3D]

? can be used to obtain information about each of those—look for example at

?Mesh

—concerning which in earlier versions of Mathematica one had to acquire a fairly good command. But Plot3D works so sweetly in v.6 that for most purposes one can forget about most of that stuff.

Spherical Plot

While Plot3D uses Cartesian coordinates to construct 3D surfaces, it is also possible to work directly in spherical coordinates, to construct 3D analogs  of polar  plots.

?SphericalPlot3D

Options[SphericalPlot3D]

First Example

SphericalPlot3D[2,{ϕ,0,π},{θ,0,π}, Boxed->False, Axes->False]

NOTE that the mouse can again be used to adjust the orientation of the figure.

Second Example

The following figure is so pretty that Bruce & Eve Torrence put it on the cover of their The Student's Introduction to Mathematica (1999).

SphericalPlot3D[θ,{ϕ,0,π}, {θ,0,((7π)/2)},Boxed->False,
Axes->False]

Use the mouse to give that a spin!

A Physical Example: Larmor Radiation Pattern

The directional dependence of the radiation emitted by an accelerated charge is (in non-relativistic approximation) given by Joseph Larmor's "sine squared" law (see Griffiths, Introduction to Electrodynamics §9.2.3).  First we plot the distribution

larmor=SphericalPlot3D[((Sin[ϕ])^2),{ϕ,0,π},{θ,0,π},
Boxed->False,
Axes->False]

Then we construct a line indicating the direction in which the charged particle has been assumed to be accelerating:

acceleration=Graphics3D[{AbsoluteThickness[2],RGBColor[1,0,0],Line[{{0,0,0},{0,0,1}}]},Boxed->False ]

And finally we superimpose the two figures:

Show[{larmor, acceleration}]

Again, give it a spin!

ListPlot3D

I have never had occasion to use this resource, which—since it can be read about at

?ListPlot3D

—I am content to pass over in silence. The following resource is, on the other hand, often indespensible.

ParametricPlot3D

?ParametricPlot3D

First Example: A Space Curve

Clear[x,y,z,t]

Let

x[t_]:=(t/10)Sin[10 t]
y[t_]:=(t/10)Cos[10 t]
z[t_]:=t (E^(-(t/10)))

describe the t-parameterized curve in 3-space (which might, for example, refer to the motion of a mathematical point or physical particle). Command

ParametricPlot3D[{x[t],y[t],z[t]},{t,0,10}]

Use the mouse to examine the curve from various angles.

Second Example: Sector of a Spherical Shell

It takes two parameters to identify the points on a surface; here we use co-latitude θ and longitude ϕ to parameterize the points on the surface of a sphere:

x[ϕ_,θ_]:=Sin[θ]Cos[ϕ]
y[ϕ_,θ_]:=Sin[θ]Sin[ϕ]
z[ϕ_,θ_]:=Cos[θ]

ParametricPlot3D[{x[ϕ,θ],y[ϕ,θ],z[ϕ,θ]},
{ϕ,0,((3π)/2)},{θ,(π/20),((19π)/20)},
Boxed->False, Axes->None]

Third Example: Torus

Here is a variant of the same basic idea:

Clear[x,y,z,u,v]

REMARK: The fact that u and v were blue before we entered the Clear command indicated that they were already clear. After execution of the command all the variables in question became blue (= clear).

We can, if we wish, consider

x[u_,v_]:=Cos[u](2+Cos[v])
y[u_,v_]:=Sin[u](2+Cos[v])
z[u_,v_]:=Sin[v]

to be components of a single vector-valued function of two parameters:

Φ[u_,v_]:={x[u,v],y[u,v],z[u,v]}

We ask Mathematica to show all the positions that such a vector can assume:

ParametricPlot3D[Φ[u,v],{u,-(1/6)π,(3/2)π},{v,0,2π},Boxed->False, Axes->None]

2D Projections of 3D Surfaces

Though my subtitle might be interpreted to refer to the screen display of a 3-dimensional surface, such as we have just been viewing, what I have in mind is more like the relation of a topographic map to the 3D model of a mountain range. In this connection, Mathematica provides two distinct resources: DensityPlot and ContourPlot.

We have already had occasion to consider how Plot3D displays the function:

f[x_,y_]:=((x^2)+3(y^2))(E^(1-(x^2)-(y^2)))

Plot3D[f[x,y],{x,-2,2},{y,-3,3}]

Look now to the following alternative displays of the same information:

DensityPlot[f[x,y],{x,-2,2},{y,-3,3}]

DensityPlot[f[x,y],{x,-2,2},{y,-3,3}, Mesh->15]

?ColorFunction

DensityPlot[f[x,y],{x,-2,2},{y,-3,3}, ColorFunction->"Rainbow"]

?ContourPlot

SUGGESTION: Go where  the ≫ takes you and look particularly to the Neat Examples.

ContourPlot[f[x,y],{x,-2,2},{y,-3,3}]

Use the mouse to make the arrow point to one of the contour lines: a little box then announces the value that the function assumes on that contour.

ContourPlot has comes with many options. Below I show the effect of exercising a couple of them:

ContourPlot[f[x,y],{x,-2,2},{y,-3,3}, ContourShading->None]

ContourPlot[f[x,y],{x,-2,2},{y,-3,3}, ContourShading->ColorData[35,"ColorList"]]

In Mathematica 6 has been enabled also to plot the solutions of equations in two variables. In previous versions of Mathematica this important capability was assigned to the (now obsolete) command ImplicitPlot, which was activated only by importation of a standard package, and never worked very well (was slow, often led to computer crashes).

ContourPlot[(x^2)+(y^2)==1, {x,-2,2},{y,-2,2}]

ContourPlot[{(x^2)+(y^1)==1,(x^2)+(y^2)==1,(x^2)+(y^3)==1,(x^2)+(y^4)==1,(x^2)+(y^5)==1,(x^2)+(y^6)==1}, {x,-2,2},{y,-2,2}]

Example: Eigenstates of a Particle in a Triangular Box

Several years ago I had occasion ("2-dimensional 'particle-in-a-box' problems in quantum mechanics" (1997)) to study the quantum mechanics of particles confined to the interior of polygonal boxes of various favorable shapes.  The 45-45-90 box led to eigenfunctions of a form which I first describe and then plot:

Clear[m,n,x,y]

boxfunction[x_,y_,m_,n_]:=Sin[m π x]Sin[n π y]-Sin[n π x]Sin[m π y]

groundstate=ContourPlot[boxfunction[x,y,1,2],{x,-1,1},{y,-1,1}]//Timing

excitedstate=ContourPlot[boxfunction[x,y,2,3],{x,-1,1},{y,-1,1}]//Timing

highlyexcitedstate=ContourPlot[boxfunction[x,y,11,15],{x,-1,1},{y,-1,1}]//Timing

The Timing command resulted in figures of reduced size, so I eliminate that command:

highlyexcitedstate=ContourPlot[boxfunction[x,y,11,15],{x,-1,1},{y,-1,1}]